package com.fzfnn.scrapbook.mapper;

import com.fzfnn.scrapbook.entity.User;
import com.fzfnn.scrapbook.mapper.provider.UserSqlProvider;
import com.fzfnn.scrapbook.vo.UserVo;
import com.fzfnn.scrapbook.vo.UserinformationVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    /*查询用户*/
    @Select("select * from user where email = #{email}")
    User selectUserByEmail(String email);
    /*用户注册*/
    @Insert("insert into user (username, password, email, created_at, status) values (#{username}, #{password}, #{email}, #{created_at}, #{status})")
    void insertUser(User user);
    /*用户登录*/
    @Select("select * from user where email and password = #{username} and #{password}")
    User loginUser(String username, String password);
    /*获取加密密码，登录时获取*/
    @Select("select password from user where email = #{email}")
    String checkPasswordByEmail(String email);
    /*查询用户信息*/
    @Select("select * from user where user_id = #{userid}")
    User selectUserByuserid(int userid);
    /*更新用户信息*/
    @UpdateProvider(type = UserSqlProvider.class, method = "updateUser")
    void updateUser(User user);
    /*更新邮箱*/
    @Update("update user set email = #{email} where user_id = #{user_id}")
    void updateEmail(User user);
    /*忘记密码*/
    @Update("update user set password = #{password} where email = #{email}")
    void forgetPassword(@Param("email") String email,@Param("password")String password);
    @Select({
            "<script>",
            "SELECT user_id AS userId, username, email,imgurl AS userAvatar,signature",
            "FROM user",
            "WHERE user_id IN",
            "<foreach item='id' collection='list' open='(' separator=',' close=')'>",
            "   #{id}",
            "</foreach>",
            "</script>"
    })
    List<UserinformationVo> selectUsersByIds(@Param("list") List<Integer> userIds);
    /*查用户*/
    @Select("select * from user where user_id = #{userId}")
    User selectUserById(Long userId);
    /*查名字*/
    @Select("select username from user where user_id = #{userId}")
    String selectUsernameById(Long userId);
    /*获取头像*/
    @Select("select imgurl from user where user_id = #{userId}")
    String getImgUrl(Long userId);
    /*双方信息*/
    @Select("SELECT u1.username AS username, u1.imgurl AS userAvatar, u2.username AS receivername, u2.imgurl AS receiverAvatar FROM user u1, user u2 WHERE  u1.user_id = #{id1} AND u2.user_id = #{id2}")
    List<UserVo> getTwoUserInfos(@Param("id1") Long id1, @Param("id2") Long id2);

    /*用户标签*/
    @Select("SELECT tag FROM user WHERE user_id = #{userId}")
    String getUserTagById(@Param("userId") Integer userId);

    @Update("UPDATE user SET tag = #{tag} WHERE user_id = #{userId}")
    void updateUserTag(@Param("userId") Integer userId, @Param("tag") String tag);

}
